const fse = require('fs-extra')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')
const ejs = require('ejs')

const { SupportedStyleSheets } = require('./consts')

module.exports = {
	apply: ({ styleSheets }) => {
		console.log(chalk.blue('生成 Prettier 配置文件...'))
		const templatePath = path.resolve(__dirname, '../template/.prettierrc')

		const data = ejs.render(fs.readFileSync(templatePath, 'utf-8'), {
			supportedStyleSheets: SupportedStyleSheets,
			styleSheets
		})

		const targetFile = path.resolve(process.cwd(), './.prettierrc')
		fse.ensureFileSync(targetFile)
		fs.writeFileSync(targetFile, data, 'utf-8')
	}
}
